package com.yuju.controller.group;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.DepartmentReturnCodeEnum;
import com.yuju.group.model.DepartmentModel;
import com.yuju.group.service.IDepartmentService;
import com.yuju.util.Utils;
import com.yuju.vo.group.DepartmentVO;
/**
 * 
 * 类名：DepartmentController  <br />
 *
 * 功能：部门相关
 *
 * @author : zhousc <br />
 * @Date : 2017年11月24日 上午9:45:40  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("department")
public class DepartmentController extends BaseController<DepartmentVO, DepartmentModel> {

	@Autowired
	private IDepartmentService departmentService;
	
	/**
	 * 
	 * 功能：<br/> 部门信息列表
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:05:13 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = departmentService.pageList(convert(departmentVO), departmentVO.getPage(), departmentVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>部门添加
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:30:05 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = departmentService.save(convert(departmentVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_SAVE_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_SAVE_FAILURE.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>部门更新
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:30:26 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = departmentService.updateSelective(convert(departmentVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_UPDATE_FAILURE.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>部门删除
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:32:02 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = departmentService.del(departmentVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_DEL_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_DEL_FAILURE.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据ID查询部门信息
	 *
	 * @author zhousc
	 * @Date 2017年11月23日 下午2:34:12 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		DepartmentModel DepartmentModel = departmentService.findById(departmentVO.getId());
		if (Utils.isNotNull(DepartmentModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(DepartmentModel);
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：重名校检<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月13日 上午10:36:29 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj)   {
		DepartmentVO departmentVO = obj.toJavaObject(DepartmentVO.class);
		Map<String, String> fail = FieldValidator.validateAll(departmentVO, "selectbyname", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		String returnString = departmentService.selectByName(convert(departmentVO));
		
		if (Utils.isNotNull(returnString)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(returnString);
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_VALID_SUCCESS.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_VALID_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(DepartmentReturnCodeEnum.DEPARTMENT_VALID_FAILURE.getCode());
			result.setReturnMsg(DepartmentReturnCodeEnum.DEPARTMENT_VALID_FAILURE.getDesc());
		}
		return result;
	}
}
